package pl.th.permission;

import android.Manifest;
import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Environment;

import java.util.LinkedList;
import java.util.List;

// 6.0及以上版本危险权限
final class RuntimePermsProcessor extends AbstractProcessor {

    RuntimePermsProcessor(String... permissions) {
        super(permissions);
    }

    @Override
    public void process(ProxyFragment proxy) {
        if (Utils.isAndroidMAndUp(Build.VERSION.SDK_INT)) {
            List<String> requested = findNotGrantedPermission(proxy);
            doLocationCompat(requested, proxy);
            doStorageCompat(requested, proxy);
            if (requested.size() == 0) {
                proxy.workOnNextProcessor();
                return;
            }
            String[] requestedArr = new String[requested.size()];
            requested.toArray(requestedArr);
            proxy.requestPermissions(requestedArr);
        } else {
            proxy.workOnNextProcessor();
        }
    }

    private List<String> findNotGrantedPermission(ProxyFragment proxy) {
        final List<String> requested = new LinkedList<>();
        for (String permission : realPermissions) {
            if (!PermissionCompat.isRuntimePermissionGranted(proxy.getActivity(), permission)) {
                String requestPermission = permission;
                if (Manifest.permission.READ_PHONE_NUMBERS.equals(permission)) {
                    if (!Utils.isAndroidOAndUp(proxy.osVersion)) {
                        requestPermission = Manifest.permission.READ_PHONE_STATE;
                    }
                } else if (Manifest.permission.BLUETOOTH_SCAN.equals(permission)) {
                    if (!Utils.isAndroidSAndUp(proxy.osVersion)) {
                        requestPermission = Manifest.permission.ACCESS_COARSE_LOCATION;
                    }
                }
                requested.add(requestPermission);
            }
        }
        return requested;
    }

    @SuppressLint("InlinedApi")
    private void doLocationCompat(List<String> requested, ProxyFragment proxy) {
        int index = requested.indexOf(Manifest.permission.ACCESS_BACKGROUND_LOCATION);
        if (index < 0) return;
        if (Utils.isAndroidRAndUp(proxy.osVersion) && Utils.isAndroidRAndUp(proxy.targetVersion)) {
            requested.remove(index);
            proxy.insertToProcessors(
                    new BGLocationPerQProcessor(Manifest.permission.ACCESS_BACKGROUND_LOCATION)
            );
        } else if (!Utils.isAndroidQAndUp(proxy.osVersion)) {
            requested.remove(index);
        }
    }

    @SuppressLint("NewApi")
    private void doStorageCompat(List<String> requested, ProxyFragment proxy) {
        if (Utils.isAndroidRAndUp(proxy.osVersion) &&
                (Environment.isExternalStorageManager() || Utils.isAndroidRAndUp(proxy.targetVersion))) {
            requested.remove(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            requested.remove(Manifest.permission.READ_EXTERNAL_STORAGE);
        }
    }
}
